<template>
  <div>
    <div class="toolbar">
      <el-input v-model="keyword" placeholder="按编码或名称搜索" style="width: 240px;" clearable />
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="success" @click="openCreate">新增院系</el-button>
    </div>

    <el-table :data="items" stripe style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="code" label="编码" width="140" />
      <el-table-column prop="name" label="名称" />
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button size="small" @click="openEdit(row)">编辑</el-button>
          <el-popconfirm title="确认删除该院系？" @confirm="doDelete(row.id)">
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="page"
      :page-size="limit"
      :total="total"
      background
      layout="prev, pager, next"
      @current-change="load"
      class="pagination"
    />

    <el-dialog v-model="dialogVisible" :title="editing ? '编辑院系' : '新增院系'" width="600">
      <el-form :model="form" label-width="100px">
        <el-form-item label="编码"><el-input v-model="form.code" /></el-form-item>
        <el-form-item label="名称"><el-input v-model="form.name" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { listDepartments, createDepartment, updateDepartment, deleteDepartment, type Department } from '@/api/grades/departments'

const loading = ref(false)
const items = ref<Department[]>([])
const total = ref(0)
const page = ref(1)
const limit = ref(10)
const keyword = ref('')

const dialogVisible = ref(false)
const editing = ref(false)
const form = ref<Department>({})

async function load() {
  loading.value = true
  try {
    const res = await listDepartments({ keyword: keyword.value || undefined, pageNum: page.value, pageSize: limit.value })
    items.value = res.items
    total.value = res.total
  } finally {
    loading.value = false
  }
}

function openCreate() {
  editing.value = false
  form.value = {}
  dialogVisible.value = true
}

function openEdit(row: Department) {
  editing.value = true
  form.value = { ...row }
  dialogVisible.value = true
}

async function submit() {
  try {
    if (editing.value && form.value.id) {
      await updateDepartment(form.value.id, form.value)
      ElMessage.success('更新成功')
    } else {
      await createDepartment(form.value)
      ElMessage.success('创建成功')
    }
    dialogVisible.value = false
    await load()
  } catch (e: any) {
    ElMessage.error(e?.message || '操作失败')
  }
}

async function doDelete(id?: number) {
  if (!id) return
  try {
    await deleteDepartment(id)
    ElMessage.success('已删除')
    await load()
  } catch (e: any) {
    ElMessage.error(e?.message || '删除失败')
  }
}

onMounted(() => {
  load()
})
</script>

<style scoped>
.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  align-items: center;
}
.pagination {
  margin-top: 12px;
  display: flex;
  justify-content: flex-end;
}
</style>